package vashion.azeroth.api.controller.task;

import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import vashion.azeroth.core.manager.item.YvogueItemWriteManager;
import vashion.azeroth.core.yvogue.pojo.YvogueProduct;
import vashion.azeroth.core.yvogue.pojo.YvogueProductList;
import vashion.azeroth.core.yvogue.sftp.SFTPUtil;
import vashion.azeroth.core.yvogue.xml.XMLUtil;

public class UpdateYvogueInventoryIncController {
	private final static Logger logger = LoggerFactory.getLogger(UpdateYvogueInventoryIncController.class);

	private Boolean isOpen;

	private String sftpHost;

	private Integer sftpPort;
	
	private String sftpUserName;

	private String sftpPassword;

	private String sftpFolder;

	private String sftpInventoryIncFileName;

	private String yvogueSupplierName;

	@Autowired
	private YvogueItemWriteManager yvogueItemWriteManager;

	public void updateYvogueInventoryInc() {
		if (isOpen) {
			try {
				logger.info("同步新增货存开始");
				SFTPUtil sftp = new SFTPUtil(sftpUserName, sftpPassword, sftpHost, sftpPort);
				sftp.login();
				String xmlStr = sftp.download(sftpFolder, sftpInventoryIncFileName);
				// System.out.println(xmlStr);
				sftp.logout();
				YvogueProductList yvogueProductList = (YvogueProductList) XMLUtil.convertXmlStrToObject(
						YvogueProductList.class, xmlStr.replaceAll("<inventory>", "").replaceAll("</inventory>", ""));
				Iterator<YvogueProduct> yvogueProductIt = yvogueProductList.iterator();
				while (yvogueProductIt.hasNext()) {
					YvogueProduct yvogueItem = (YvogueProduct) yvogueProductIt.next();
					yvogueItemWriteManager.publishItem(yvogueItem, yvogueSupplierName);
				}
				logger.info("同步新增货存结束");
			} catch (Exception e) {
				logger.error("同步新增货存错误：" + e.getMessage());
				e.printStackTrace();
			}
		}
	}

	public Boolean getIsOpen() {
		return isOpen;
	}

	public void setIsOpen(Boolean isOpen) {
		this.isOpen = isOpen;
	}

	public String getSftpHost() {
		return sftpHost;
	}

	public void setSftpHost(String sftpHost) {
		this.sftpHost = sftpHost;
	}

	public Integer getSftpPort() {
		return sftpPort;
	}

	public void setSftpPort(Integer sftpPort) {
		this.sftpPort = sftpPort;
	}

	public String getSftpUserName() {
		return sftpUserName;
	}

	public void setSftpUserName(String sftpUserName) {
		this.sftpUserName = sftpUserName;
	}

	public String getSftpPassword() {
		return sftpPassword;
	}

	public void setSftpPassword(String sftpPassword) {
		this.sftpPassword = sftpPassword;
	}

	public String getSftpFolder() {
		return sftpFolder;
	}

	public void setSftpFolder(String sftpFolder) {
		this.sftpFolder = sftpFolder;
	}

	public String getSftpInventoryIncFileName() {
		return sftpInventoryIncFileName;
	}

	public void setSftpInventoryIncFileName(String sftpInventoryIncFileName) {
		this.sftpInventoryIncFileName = sftpInventoryIncFileName;
	}

	public String getYvogueSupplierName() {
		return yvogueSupplierName;
	}

	public void setYvogueSupplierName(String yvogueSupplierName) {
		this.yvogueSupplierName = yvogueSupplierName;
	}

}
